﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TermEquation
{
  public class MainTask : ITask
  {
    public const double ksi = 1.0 / 3.0;
    public const double mu1 = 1;
    public const double mu2 = 2;

    public double a(int i, double h)
    {
      if (i * h <= ksi)
      {
        double x1 = (i) * h;
        double x0 = (i - 1) * h;
        return h / (Math.Log(x1 + 1,Math.E)- Math.Log(x0 + 1,Math.E));
      }
      else
      {
        if ((i - 1) * h >= ksi)
          return 1;
        else
        {
          double x1 = (i) * h;
          double x0 = (i - 1) * h;
          return h / (Math.Log(ksi + 1,Math.E)- Math.Log(x0 + 1,Math.E) + x1 - ksi);
        }
      }
    }

    public double d(int i, double h)
    {
      if ((i + 1 / 2.0) * h <= ksi)
      {
        double x1 = (i + 1 / 2.0) * h;
        double x0 = (i - 1 / 2.0) * h;
        return (1 / h) * (x1 * x1 * x1 / 3.0 + x1 - x0 * x0 * x0 / 3.0 - x0);
      }
      else
      {
        if ((i - 1 / 2.0) * h >= ksi)
        {
          double x1 = (i + 1 / 2.0) * h;
          double x0 = (i - 1 / 2.0) * h;
          return (x1 * x1 - x0 * x0) / h;
        }
        else
        {
          double x1 = (i + 1 / 2.0) * h;
          double x0 = (i - 1 / 2.0) * h;
          return 1 / h * (ksi * ksi * ksi / 3.0 + ksi - x0 * x0 * x0 / 3.0 - x0 + x1 * x1 - ksi * ksi);
        }
      }
    }

    public double fi(int i, double h)
    {
      if ((i + 1 / 2.0) * h <= ksi)
      {
        return 1;
      }
      else
      {
        if ((i - 1 / 2.0) * h >= ksi)
        {
          double x1 = (i + 1 / 2.0) * h;
          double x0 = (i - 1 / 2.0) * h;
          return (1 / h) * (x1 * x1 * x1 / 3.0 - x1 - x0 * x0 * x0 / 3.0 + x0);
        }
        else
        {
          double x1 = (i + 1 / 2.0) * h;
          double x0 = (i - 1 / 2.0) * h;
          return (ksi - (i - 1 / 2.0) * h) / h + (x1 * x1 * x1 / 3.0 - x1 - ksi * ksi * ksi / 3.0 + ksi) / h;
        }
      }
    }

    public static double k1(double x)
    {
      return x + 1;
    }

    public static double k2(double x)
    {
      return 1;
    }

    public static double q1(double x)
    {
      return x * x + 1;
    }

    public static double q2(double x)
    {
      return 2 * x;
    }

    public static double f1(double x)
    {
      return 1;
    }

    public static double f2(double x)
    {
      return x * x - 1;
    }
  }
}